package com.aruba.service.impl;

import com.aruba.bean.Dept;
import com.aruba.mapper.DeptMapper;
import com.aruba.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private RedisTemplate<String, List<Dept>> redisTemplate;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> findAllDept() {
        // 先从redis缓存中取
        List<Dept> deptList = redisTemplate.opsForValue().get("dept");
        if (deptList != null) {
            System.out.println("从缓存中取");
            return deptList;
        }

        System.out.println("从数据库中取");
        List<Dept> allDept = deptMapper.findAllDept();
        redisTemplate.opsForValue().set("dept", allDept);
        return allDept;
    }

}
